package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    /**
     * 查询购物车
     */
    @Autowired
    ShoppingCartService shoppingCartService;

    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        //从线程中获取用户id
        Long userId = BaseContext.get();
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return R.success(list);
    }

    /**
     * 新增购物车
     */

    @PostMapping("add")
    public R add(@RequestBody ShoppingCart shoppingCart) {

        Long userId = BaseContext.get();

        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if (one != null) {
            //说明有数据需要进行修改
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            return R.success(one);

        }
        shoppingCart.setUserId(userId);
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }
    /**
    * 清空购物车
    */
    @DeleteMapping("clean")
    public R clean(){

        Long userId = BaseContext.get();

        //根据用户ID进行删除数据
        shoppingCartService.lambdaUpdate()
                .eq(ShoppingCart::getUserId,userId)
                .remove();

        return R.success("清空成功");
    }

    /**
     * -购物车
     */
    @PostMapping("sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){

        Long userId = BaseContext.get();
        //根据用户和菜品或者套餐id获取数量的数字
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();


        if(one.getNumber()>1){

            //如果大于1就进行修改
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);

        }else {
            //如果<1就进行删除
            shoppingCartService.lambdaUpdate()
                    .eq(ShoppingCart::getUserId,userId)
                    .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                    .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                    .remove();
            one.setNumber(0);
        }
       return R.success(one);
    }


}
